package com.example.mybatisplus.web.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.UserAccount;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.dto.SelectedIdsDTO;
import com.example.mybatisplus.service.UserAccountService;


/**
 *
 *  前端控制器
 *
 *
 * @author cft
 * @since 2022-06-24
 * @version v1.0
 */
@Controller
@RequestMapping("/api/userAccount")
public class UserAccountController {

    private final Logger logger = LoggerFactory.getLogger( UserAccountController.class );

    @Autowired
    private UserAccountService userAccountService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        UserAccount  userAccount =  userAccountService.getById(id);
        return JsonResponse.success(userAccount);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        userAccountService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateUserAccount(UserAccount  userAccount) throws Exception {
        userAccountService.updateById(userAccount);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建UserAccount
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
	public JsonResponse create(UserAccount userAccount) throws Exception {
		userAccountService.save(userAccount);
		return JsonResponse.success(null);
	}
	
	//实现登录的接口
	@RequestMapping("/login")
	@ResponseBody
	public JsonResponse login(@RequestBody UserAccount userAccount) {
		UserAccount user = userAccountService.login(userAccount);

		//把登录用户存进session中
		if (user != null) {
			SessionUtils.saveCurrentUserInfo(user);
		}
		return JsonResponse.success(user);
	}


	/* 
	 * author:cft
	 * function:实现修改密码的接口
	 */
	@RequestMapping("/changePassword")
	@ResponseBody
	public JsonResponse changePassword(@RequestBody JSONObject jsonpObject) {
		UserAccount newUser = userAccountService.changePassword(jsonpObject.getString("oldPassword"),
				jsonpObject.getString("newPassword"));
		//将登录用户新的信息存入session中
		if (newUser != null) {
			SessionUtils.saveCurrentUserInfo(newUser);
			return JsonResponse.success(newUser);
		} else
			return JsonResponse.failure("旧密码错误！");
	}


	/* 
	 * author:cft
	 * function:实现查询咨询师的信息
	 */
	@ResponseBody
	@RequestMapping("/getConInfo")
	public JsonResponse getConInfo(PageDTO pageDTO, UserAccount userAccount) {
		Page<UserAccount> uPage = userAccountService.getConInfoList(pageDTO, userAccount);
		return JsonResponse.success(uPage);
	}
	
	/* 
	 * author:cft
	 * function:实现删除选中的咨询师信息
	 */
	@PostMapping("/deleteSelectedCon")
	@ResponseBody
	public JsonResponse deleteSeletecdCon(@RequestBody SelectedIdsDTO deleteConDTO) {
		userAccountService.removeByIds(deleteConDTO.getIdSelected());
		return JsonResponse.success("successfully delete!");
	}
	

	/* 
	 * author:cft
	 * function:实现修改选中的咨询师信息
	 */
	@RequestMapping("/alterConInfo")
	@ResponseBody
	public JsonResponse alterConInfo(@RequestBody UserAccount user) {
		userAccountService.updateById(user);
		return JsonResponse.success("successfully altered!");
	}

	/* 
	 * author:cft
	 * function:获得所有咨询师的姓名和id
	 */
	@RequestMapping("/getAllConNameAndId")
	@ResponseBody
	public JsonResponse getAllConNameAndId() {
		List<UserAccount> list = userAccountService.getAllConNameAndId();
		return JsonResponse.success(list);
	}


	/* 
	 * author:cft
	 * function:得到所有初访员的姓名和id
	 */
	@RequestMapping("/getAllFViewerNameAndId")
	@ResponseBody
	public JsonResponse getAllFViewerNameAndId() {
		List<UserAccount> list = userAccountService.getAllFViewerNameAndId();
		return JsonResponse.success(list);
	}

	
	/*
	* author:tqq
	* 实现通过id查询userName
	* */
	@PostMapping("/getUserName")
	@ResponseBody
	public JsonResponse getUserName(@RequestBody JSONObject json){
		Long str=Long.parseLong(json.getString("userId"));
		String name = userAccountService.getUserName(str);
		return JsonResponse.success(name);
	}
}

